package com.wngbms.netty.seatdemo.service;

import com.alibaba.fastjson.JSON;
import com.wngbms.netty.seatdemo.bean.SeatEvent;
import com.wngbms.netty.seatdemo.comm.*;
import com.wngbms.netty.seatdemo.message.BaseHeader;
import com.wngbms.netty.seatdemo.message.SeatInMessage;
import com.wngbms.netty.seatdemo.message.SeatOutMessage;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.UUID;

@Slf4j
public class HeartBeatHandler implements SeatTypeHandler {

    public static final CmdType CMD_TYPE = CmdType.HEARTBEAT;

    @Override
    public void handleInbound(SeatInMessage seatInMessage) {
        log.info("收到心跳");

        /*SeatEvent seatEvent = new SeatEvent();
        seatEvent.setEventId("心跳");
        seatEvent.setEventTime(new Date());
        seatEvent.setMediaId("心跳");
        seatEvent.setSubsystemCode("心跳");
        byte[] bodyBytes = JSON.toJSONString(seatEvent).getBytes(StandardCharsets.UTF_8);
        BaseHeader baseHeader = BaseHeader.build(MagicVersion.V1,
                CmdType.EVENT, BodyType.NON,
                EncryptionType.NON, ReqOrResp.RESP, bodyBytes.length);

        SeatOutMessage seatOutMessage = new SeatOutMessage();
        seatOutMessage.setBaseHeader(baseHeader);
        seatOutMessage.setBody(bodyBytes);*/

        BaseHeader baseHeader = BaseHeader.build(MagicVersion.V1,
                CmdType.HEARTBEAT, BodyType.NON,
                EncryptionType.NON, ReqOrResp.RESP, 0);

        SeatOutMessage seatOutMessage = new SeatOutMessage();
        seatOutMessage.setBaseHeader(baseHeader);

        ChannelHandlerContext channelHandlerContext = seatInMessage.getChannelHandlerContext();
        channelHandlerContext.channel()
                .writeAndFlush(seatOutMessage)
                .addListener(future -> log.info("响应心跳"));
    }

}
